package top.yokey.shopnc.activity.seller;

import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.RelativeLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import top.yokey.base.base.SellerHttpClient;
import top.yokey.shopnc.base.BaseCountTime;
import top.yokey.shopnc.base.BaseImageLoader;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.base.util.JsonUtil;
import top.yokey.shopnc.R;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.shopnc.base.BaseShared;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.base.bean.SellerIndexBean;
import top.yokey.base.model.SellerIndexModel;

/**
 * @author MapStory
 * @ qq 1002285057
 * @ Project https://gitee.com/MapStory/ShopNc-Android
 */

@ContentView(R.layout.activity_seller_seller)
public class SellerActivity extends SwipeActivity {

    @ViewInject(R.id.mainScrollView)
    private NestedScrollView mainScrollView;

    @ViewInject(R.id.avatarImageView)
    private AppCompatImageView avatarImageView;
    @ViewInject(R.id.nicknameTextView)
    private AppCompatTextView nicknameTextView;
    @ViewInject(R.id.saleDayTextView)
    private AppCompatTextView saleDayTextView;
    @ViewInject(R.id.saleMonthTextView)
    private AppCompatTextView saleMonthTextView;
    @ViewInject(R.id.saleIngTextView)
    private AppCompatTextView saleIngTextView;

    @ViewInject(R.id.orderRelativeLayout)
    private RelativeLayout orderRelativeLayout;
    @ViewInject(R.id.orderNewRelativeLayout)
    private RelativeLayout orderNewRelativeLayout;
    @ViewInject(R.id.orderPayRelativeLayout)
    private RelativeLayout orderPayRelativeLayout;
    @ViewInject(R.id.orderSendRelativeLayout)
    private RelativeLayout orderSendRelativeLayout;
    @ViewInject(R.id.orderSuccessRelativeLayout)
    private RelativeLayout orderSuccessRelativeLayout;
    @ViewInject(R.id.orderNewDotTextView)
    private AppCompatTextView orderNewDotTextView;
    @ViewInject(R.id.orderPayDotTextView)
    private AppCompatTextView orderPayDotTextView;

    @ViewInject(R.id.goodsRelativeLayout)
    private RelativeLayout goodsRelativeLayout;
    @ViewInject(R.id.goodsIngTextView)
    private AppCompatTextView goodsIngTextView;
    @ViewInject(R.id.goodsWareTextView)
    private AppCompatTextView goodsWareTextView;
    @ViewInject(R.id.goodsIllegalTextView)
    private AppCompatTextView goodsIllegalTextView;
    @ViewInject(R.id.goodsAddTextView)
    private AppCompatTextView goodsAddTextView;

    @ViewInject(R.id.addressRelativeLayout)
    private RelativeLayout addressRelativeLayout;
    @ViewInject(R.id.logisticsRelativeLayout)
    private RelativeLayout logisticsRelativeLayout;
    @ViewInject(R.id.settingRelativeLayout)
    private RelativeLayout settingRelativeLayout;

    private SellerIndexBean sellerIndexBean;

    @Override
    public void initData() {

        getData();

    }

    @Override
    public void initEven() {

        orderRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startOrderSeller(getActivity(), 0);
            }
        });

        orderNewRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startOrderSeller(getActivity(), 0);
            }
        });

        orderPayRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startOrderSeller(getActivity(), 1);
            }
        });

        orderSendRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startOrderSeller(getActivity(), 2);
            }
        });

        orderSuccessRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startOrderSeller(getActivity(), 3);
            }
        });

        goodsRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startGoodsSeller(getActivity(), 0);
            }
        });

        goodsIngTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startGoodsSeller(getActivity(), 0);
            }
        });

        goodsWareTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startGoodsSeller(getActivity(), 1);
            }
        });

        goodsIllegalTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startGoodsSeller(getActivity(), 2);
            }
        });

        goodsAddTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckSellerLogin(getActivity(), GoodsAddActivity.class);
            }
        });

        addressRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckSellerLogin(getActivity(), AddressActivity.class);
            }
        });

        logisticsRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckSellerLogin(getActivity(), ExpressActivity.class);
            }
        });

        settingRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckSellerLogin(getActivity(), SettingActivity.class);
            }
        });

    }

    //自定义方法

    private void getData() {

        SellerIndexModel.get().index(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                String data = baseBean.getDatas().replace("null", "\"\"");
                sellerIndexBean = JsonUtil.json2Bean(data, SellerIndexBean.class);
                BaseImageLoader.get().displayCircle(sellerIndexBean.getStoreInfo().getStoreAvatar(), avatarImageView);
                String temp = sellerIndexBean.getSellerInfo().getSellerName() + "（" + sellerIndexBean.getStoreInfo().getGradeName() + "）";
                nicknameTextView.setText(temp);
                temp = sellerIndexBean.getStoreInfo().getDailySales().getOrdernum() + "\n昨日销量";
                saleDayTextView.setText(temp);
                temp = sellerIndexBean.getStoreInfo().getMonthlySales().getOrdernum() + "\n当月销量";
                saleMonthTextView.setText(temp);
                temp = sellerIndexBean.getStatics().getOnline() + "\n出售中";
                saleIngTextView.setText(temp);
                orderNewDotTextView.setVisibility(sellerIndexBean.getStatics().getPayment().equals("0") ? View.GONE : View.VISIBLE);
                orderPayDotTextView.setVisibility(sellerIndexBean.getStatics().getDelivery().equals("0") ? View.GONE : View.VISIBLE);
            }

            @Override
            public void onFailure(String reason) {
                if (reason.equals("请登录")) {
                    BaseToast.get().show("身份信息已失效，请重新登录！");
                    SellerHttpClient.get().updateKey("");
                    BaseShared.get().putString(BaseConstant.SHARED_SELLER_KEY, "");
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    BaseApplication.get().finish(getActivity());
                    return;
                }
                BaseSnackBar.get().show(mainScrollView, reason);
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getData();
                    }
                }.start();
            }
        });

    }

}
